<?php
global $wpdb;
if(isset($_GET['del']))
{
	$url = get_bloginfo('home')."/wp-admin/admin.php?page=tour-setting&action=tour-price-table";
	$del = $_GET['del'];
	db_delete_item_price_by_id($del, "tour_price");
	wp_redirect($url);
}

if(isset($_POST['price_table_tour']) && $_POST['price_table_price']!=null)
	$_SESSION['current_tour_price_table'] = $_POST['price_table_tour'];
//print_r($_POST);
if(wp_verify_nonce($_POST['act_insert_tour_price_table'],'act_insert_tour_price_table'))
{
	$_SESSION['current_tour_price_table'] = $_POST['price_table_tour'];
	if(isset($_POST['insert-tour-price-table']))
	{	
		if($_POST['travel-in-party-of'] !="" || $_POST['standard'] != "" || $_POST['superior'] != "" )
		{
			$insert_code = $_POST['tour_code'];
			if(intval($_POST['tour_code'])==intval($_SESSION['insert_code']))
				db_insert_tour_price_table($_POST);
		}
	}
	else if(isset($_POST['update-tour-price-table']))
	{
		$_SESSION['current_tour_price_table'] = $_POST['price_table_tour2'];
		db_update_tour_price_table($_POST);
	}
} 
$code = rand(1111,9999);
$_SESSION['insert_code'] = $code;
?>
<style>
.hienthi
{
	display:block;
}
.an
{
	display:none;
}
</style>
<div id="index_page">
	<form action="" method="post" name="insert-price-table">
		<table>
			<tr class="first-header">
				<td colspan="6">
				<?php
				$hotels = db_query_all_post_in_post_type(-1, 'tour', 'date', 'DESC', $wpdb->prefix.'display', 'display');
				query_posts($hotels); 
				?>
				<select onchange="this.form.submit()" style="min-width: 400px;" name="price_table_tour" id="price_table_tour">
					<option>Select Tour</option>
					<?php
					while (have_posts())
					{
						the_post();
						$postID = get_the_ID();
						if(isset($_SESSION['current_tour_price_table']) && intval($_SESSION['current_tour_price_table'])==$postID)
						{
							?>
							<option selected="selected" value="<?php echo $postID;?>"><?php the_title();?></option>
							<?php
						}
						else 
						{
						?>
							<option value="<?php echo $postID;?>"><?php the_title();?></option>
						<?php	
						} 
					} 
					?>
				</select>
				
				
				<?php
				query_posts($hotels);  
				?>
				<select style="min-width: 400px;" name="price_table_tour2" id="price_table_tour2" class="an">
					<option>Select Tour</option>
					<?php
					while (have_posts())
					{
						the_post();
						$postID = get_the_ID();
						if(isset($_SESSION['current_tour_price_table']) && intval($_SESSION['current_tour_price_table'])==$postID)
						{
							?>
							<option selected="selected" value="<?php echo $postID;?>"><?php the_title();?></option>
							<?php
						}
						else 
						{
						?>
							<option value="<?php echo $postID;?>"><?php the_title();?></option>
						<?php	
						} 
					} 
					?>
				</select>
				</td>
			</tr>
			<tr class="first-header">
				<td> Travel in party of</td>
				<td> Standard <br/><span style="font-style: italic; font-weight: normal; color: red; font-size: 11px;">Number</span></td>
				<td> Superior <br/><span style="font-style: italic; font-weight: normal; color: red; font-size: 11px;">Number</span></td>
				<td> First Class <br/><span style="font-style: italic; font-weight: normal; color: red; font-size: 11px;">Number</span></td>
				<td> Deluxe <br/><span style="font-style: italic; font-weight: normal; color: red; font-size: 11px;">Number</span></td>
				<td> Insert </td>
			</tr>
			<tr class="second-item">
				<td>
					<input type="hidden" name="tourID_hidden" id="tourID_hidden" value="">
					<select id="travel-in-party-of" name="travel-in-party-of">
					<?php
                                	$travel_in_list = db_travel_in_party_of();
                                	foreach ($travel_in_list as $key=>$value) 
                                	{
                                	?>
                                		<option value="<?php echo $value;?>"><?php echo $value;?></option>
                                	<?php 	
                                	} 
                                	?>
					</select>
				 	<!-- <input id="travel-in-party-of" type="text" value="" name="travel-in-party-of"> --> 
				 </td>
				<td> <input id="standard" 			type="text" value="" name="standard"> </td>
				<td> <input id="superior" 			type="text" value="" name="superior"> </td>
				<td> <input id="first-class" 		type="text" value="" name="first-class"> </td>
				<td> <input id="deluxe" 			type="text" value="" name="deluxe">
					<input type="hidden" value="<?php echo $code;?>" name="tour_code">
				 </td>
				<td> 
					<ul>
						<li class="sbInsert">
							<input type="submit" class="tour_insert_price" value="Insert" name="insert-tour-price-table">
						</li>
						<li class="sbUpdate">
							<input type="submit" class="tour_update_price_hidden" value="Update" name="update-tour-price-table">
						
							<input type="submit" class="tour_update_price_hidden" value="return" name="return-tour-price-table">
						</li>
					</ul>
				</td>				
			</tr>			
		</table>
		<?php wp_nonce_field('act_insert_tour_price_table','act_insert_tour_price_table');?>
	</form>
	<!-- ************************************************************* -->
	<?php
	//$list_price = db_select_all_data("tour_price");
	$tourID = intval($_SESSION['current_tour_price_table']);
	$list_price = db_select_all_data_by_tourID("tour_price",$tourID);
	//print_r($list_price); 
	?>
	<br/>
	<table>
			
			<tr class="first-header">
				<td> Travel in party of </td>
				<td> Standard </td>
				<td> Superior </td>
				<td> First Class </td>
				<td> Deluxe </td>
				<td> Actions </td>				
			</tr> 
			<?php
			if($list_price)
			{
			foreach ($list_price as $value) 
			{
			?>
			<tr class="item-second">
				<td> <?php echo $value->travelinparty;?></td>
				<td> <?php echo $value->standard;?> USD</td>
				<td> <?php echo $value->superior;?> USD</td>
				<td> <?php echo $value->firstclass;?> USD</td>
				<td> <?php echo $value->deluxe;?> USD</td>
				<td>
				<?php
					$str = $value->ID.'-'.$value->travelinparty.'-'.$value->standard.'-'.$value->superior.'-'.$value->firstclass.'-'.$value->deluxe;  
				?>
					<a class="update_tour" rel="<?php echo $str;?>" href="#">Update</a> &nbsp;|&nbsp;
					<a href="<?php bloginfo('home');?>/wp-admin/admin.php?page=tour-setting&action=tour-price-table&del=<?php echo $value->ID;?>">Delete</a>
				</td>								
			</tr>
			<?php 
			}
			} 
			?>
		</table>	
</div>
